<template>
  <div id="tab-bar-item" @click="itemClick">
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <!-- 插槽最好都包装一份 -->
    <!-- 用层叠性把属性继承过来 -->
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props: {
    //向子组件传递数据
    link: String,
    activeColor: {
      type: String,
      default: "red"
    }
  },
  data() {
    return {
      //isActive: false
    };
  },
  computed: {
    isActive() {
      return this.$route.path.indexOf(this.link) !== -1; //获取当前点击的div的路径，和我们所有item的值匹配，找到了当前的就是ture 通过动态属性计算
    },
    activeStyle() {
      return this.isActive ? { color: this.activeColor } : {};
    }
  },
  methods: {
    itemClick() {
      console.log(this.link);

      this.$router.replace(this.link);
    }
  }
};
</script>

<style>
#tab-bar-item {
  flex: 1;
  height: 49px;
  text-align: center;
  background-color: #f6f6f6;
}
#tab-bar-item img {
  margin-top: 3px;
  margin-bottom: 2px;
  width: 24px;
  height: 24px;
  vertical-align: middle;
}
#tab-bar-item div {
  font-size: 14px;
}
</style>